package com.recource.controller;

import com.recource.annonation.Login;
import com.recource.dao.UserDao;
import com.recource.domain.Users;
import com.recource.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Objects;
import java.util.UUID;

/**
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping("/detail")
    public String detail(){
        return "/admin/my/detail";
    }

    @Autowired
    HostHolder hostHolder;

    @Autowired
    private UserDao userDao;

    @RequestMapping("/my/update")
    @Login
    //更新个人信息
    public String detailupdate(Users ro){
        Users users = hostHolder.getUser();
        if(!Objects.equals(users.getId(), ro.getId())){
            throw new RuntimeException("用户不对应");
        }
        //查找用户是否存在
        //存在就更新密码和信息
        String salt = UUID.randomUUID().toString().substring(0, 5);
        users.setSalt(salt);
        StringBuilder sb = new StringBuilder();
        String password = DigestUtils.md5DigestAsHex((sb.append(salt).append(ro.getPassword())).toString().getBytes());
        users.setPassword(password);
        users.setUserType(ro.getUserType());
        users.setPhone(ro.getPhone());
        users.setUsername(ro.getUsername());
        users.setIdCard(ro.getIdCard());
        users.setEmail(ro.getEmail());
        users.setSex(ro.getSex());
        users.setAddress(ro.getAddress());
        users.setDate(ro.getDate());
        userDao.updateById(users);
        //更新成功后进入首页
        return "redirect:/admin/index";
    }


}